/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/*
 * LoginForm.java
 *
 * Created on 16/09/2010, 13:47:12
 */
package NajaTaskClient.interfaces.comum;

import NajaTaskClient.classes.NajaTask;
import javax.swing.JDialog;
import javax.swing.JFrame;
import javax.swing.JOptionPane;

/**
 *
 * @author alex.silva
 */
public class TelaDeLogin extends javax.swing.JDialog {

    JFrame JFrameTelaPrincipal;
    JDialog ConfigurarConexao;

    /** Creates new form LoginForm */
    public TelaDeLogin(java.awt.Frame parent, boolean modal) {
        super(parent, modal);


        ConfigurarConexao = new configurarConexao(parent, true);

        initComponents();
        this.setLocationRelativeTo(getRootPane());
    }

    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        panelLogin = new javax.swing.JPanel();
        buttonEntrar = new javax.swing.JButton();
        labelUsuario = new javax.swing.JLabel();
        textfieldUsuario = new javax.swing.JTextField();
        labelSenha = new javax.swing.JLabel();
        buttonSair = new javax.swing.JButton();
        buttonConfigurarConexao = new javax.swing.JButton();
        jPasswordSenha = new javax.swing.JPasswordField();

        setDefaultCloseOperation(javax.swing.WindowConstants.DISPOSE_ON_CLOSE);
        setTitle("JavaTarefas - Login");
        addWindowListener(new java.awt.event.WindowAdapter() {
            public void windowClosed(java.awt.event.WindowEvent evt) {
                sair(evt);
            }
        });

        buttonEntrar.setIcon(new javax.swing.ImageIcon(getClass().getResource("/NajaTaskClient/recursos/icons/lock_go.png"))); // NOI18N
        buttonEntrar.setMnemonic('E');
        buttonEntrar.setText("Entrar");
        buttonEntrar.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                buttonEntrarActionPerformed(evt);
            }
        });

        labelUsuario.setText("Usuárido");

        labelSenha.setText("Senha");

        buttonSair.setIcon(new javax.swing.ImageIcon(getClass().getResource("/NajaTaskClient/recursos/icons/exit.png"))); // NOI18N
        buttonSair.setMnemonic('S');
        buttonSair.setText("Sair");
        buttonSair.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                buttonSairActionPerformed(evt);
            }
        });

        buttonConfigurarConexao.setIcon(new javax.swing.ImageIcon(getClass().getResource("/NajaTaskClient/recursos/icons/lock_go.png"))); // NOI18N
        buttonConfigurarConexao.setMnemonic('C');
        buttonConfigurarConexao.setText("Configurar conexao");
        buttonConfigurarConexao.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                buttonConfigurarConexaoActionPerformed(evt);
            }
        });

        jPasswordSenha.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jPasswordSenhaActionPerformed(evt);
            }
        });

        javax.swing.GroupLayout panelLoginLayout = new javax.swing.GroupLayout(panelLogin);
        panelLogin.setLayout(panelLoginLayout);
        panelLoginLayout.setHorizontalGroup(
            panelLoginLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(panelLoginLayout.createSequentialGroup()
                .addContainerGap()
                .addGroup(panelLoginLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(panelLoginLayout.createSequentialGroup()
                        .addGroup(panelLoginLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addComponent(labelUsuario)
                            .addComponent(labelSenha))
                        .addGap(18, 18, 18)
                        .addGroup(panelLoginLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addComponent(jPasswordSenha, javax.swing.GroupLayout.PREFERRED_SIZE, 197, javax.swing.GroupLayout.PREFERRED_SIZE)
                            .addComponent(textfieldUsuario, javax.swing.GroupLayout.DEFAULT_SIZE, 277, Short.MAX_VALUE)))
                    .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, panelLoginLayout.createSequentialGroup()
                        .addComponent(buttonConfigurarConexao)
                        .addGap(18, 18, 18)
                        .addComponent(buttonSair)
                        .addGap(18, 18, 18)
                        .addComponent(buttonEntrar)))
                .addContainerGap())
        );
        panelLoginLayout.setVerticalGroup(
            panelLoginLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(panelLoginLayout.createSequentialGroup()
                .addContainerGap()
                .addGroup(panelLoginLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(labelUsuario)
                    .addComponent(textfieldUsuario, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addGroup(panelLoginLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(labelSenha)
                    .addComponent(jPasswordSenha, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                .addGroup(panelLoginLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(buttonEntrar)
                    .addComponent(buttonSair)
                    .addComponent(buttonConfigurarConexao))
                .addContainerGap())
        );

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addComponent(panelLogin, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addComponent(panelLogin, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
        );

        pack();
    }// </editor-fold>//GEN-END:initComponents

    private void buttonEntrarActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_buttonEntrarActionPerformed
        // TODO add your handling code here:

        if (this.textfieldUsuario.getText().equals("")) {
            JOptionPane.showMessageDialog(this,
                    "Digite o nome de usuário para continuar",
                    "Campo vazio",
                    JOptionPane.ERROR_MESSAGE);
        } else if (this.jPasswordSenha.getText().equals("")) {
            JOptionPane.showMessageDialog(this,
                    "Digited a senha para continuar",
                    "Camdpo dvazio",
                    JOptionPane.ERROR_MESSAGE);
        } else {
            //verifica se usuario existe no banco de dados

            if (NajaTask.get().login(this.textfieldUsuario.getText(), this.jPasswordSenha.getText())) {
                this.exibeTelaPrincipal();
                this.setVisible(false);
            }else{
                 JOptionPane.showMessageDialog(null,
                    "Não foi possível efetuar a conexão com esse nome de usuário e/ou senha , verifique os dados digitados e tente novamente",
                    "Erro de conexão",
                    JOptionPane.ERROR_MESSAGE);
            }
            //


        }

    }//GEN-LAST:event_buttonEntrarActionPerformed

    private void buttonSairActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_buttonSairActionPerformed
        dispose();
        System.exit(0); //calling the method is a must

    }//GEN-LAST:event_buttonSairActionPerformed

    private void buttonConfigurarConexaoActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_buttonConfigurarConexaoActionPerformed
        this.exibeConfigurarConexao();
    }//GEN-LAST:event_buttonConfigurarConexaoActionPerformed

    private void sair(java.awt.event.WindowEvent evt) {//GEN-FIRST:event_sair
        System.exit(0);
    }//GEN-LAST:event_sair

    private void jPasswordSenhaActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jPasswordSenhaActionPerformed
        // TODO add your handling code here:
    }//GEN-LAST:event_jPasswordSenhaActionPerformed

    public void exibeTelaPrincipal() {
        this.JFrameTelaPrincipal.setVisible(true);
    }

    /**
     * @param args the command line arguments
     */
    public static void main(String args[]) {
        java.awt.EventQueue.invokeLater(new Runnable() {

            public void run() {
                TelaDeLogin dialog = new TelaDeLogin(new javax.swing.JFrame(), true);
                dialog.addWindowListener(new java.awt.event.WindowAdapter() {

                    public void windowClosing(java.awt.event.WindowEvent e) {
                        System.exit(0);
                    }
                });
                dialog.setVisible(true);
            }
        });
    }

    private void exibeConfigurarConexao() {
        ConfigurarConexao.setVisible(true);
    }

    public void telaPrincipal(JFrame home) {
        this.JFrameTelaPrincipal = home;
    }
    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton buttonConfigurarConexao;
    private javax.swing.JButton buttonEntrar;
    private javax.swing.JButton buttonSair;
    private javax.swing.JPasswordField jPasswordSenha;
    private javax.swing.JLabel labelSenha;
    private javax.swing.JLabel labelUsuario;
    private javax.swing.JPanel panelLogin;
    private javax.swing.JTextField textfieldUsuario;
    // End of variables declaration//GEN-END:variables
}
